package com.example.musictutor.repository;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.musictutor.conf.Constants;

/**
 * Created by ironhulk on 2014/05/05.
 */
public class DBHelper extends SQLiteOpenHelper{
    Context context;

    public DBHelper(Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.DB_VERSION);
        this.context = context;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
          db.execSQL("CREATE TABLE "+ Constants.TABLE_USER_PROGRESS +" ( " + Constants.C_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ Constants.C_AMOUNT_OF_ATTEMPTS +" INTEGER,"+ Constants.C_AMOUNT_PASSED +" INTEGER,"+ Constants.C_AMOUNT_FAILED +" INTEGER," + Constants.C_LEVEL_ID + " INTEGER," + Constants.C_SONG_ID + " INTEGER);");

          db.execSQL("CREATE TABLE "+ Constants.TABLE_SONG +" ( " + Constants.C_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ Constants.C_NAME +" TEXT,"+ Constants.C_TEMPO +" INTEGER," + Constants.C_HIGH_SCORE + " INTEGER," + Constants.C_UNLOCKED +" INTEGER);");
          
          db.execSQL("CREATE TABLE "+ Constants.TABLE_NOTE +" ( " + Constants.C_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ Constants.C_NAME +" TEXT,"+ Constants.C_RESOURCE_ID +" INTEGER);");
          
          db.execSQL("CREATE TABLE "+ Constants.TABLE_SONG_NOTE +" ( " + Constants.C_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ Constants.C_SONG_ID +" INTEGER,"+ Constants.C_NOTE_ID +" INTEGER, "+ Constants.C_START_POSITION +" INTEGER, "+ Constants.C_END_POSITION +" INTEGER);");
          
          Log.d(Constants.LOG_TAG, "Created Tables " + Constants.TABLE_USER_PROGRESS);
          Log.d(Constants.LOG_TAG, "Created Tables " + Constants.TABLE_SONG);
          Log.d(Constants.LOG_TAG, "Created Tables " + Constants.TABLE_NOTE);
          Log.d(Constants.LOG_TAG, "Created Tables " + Constants.TABLE_SONG_NOTE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_USER_PROGRESS);
        db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_SONG);
        db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NOTE);
        db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_SONG_NOTE);
        onCreate(db);
    }
}
